/* eslint-disable no-unused-vars */
import React, { useState, useEffect } from 'react';
import Cookies from 'js-cookie';
import qs from 'qs';
import { useHistory } from 'react-router-dom';
import { Affix, Drawer, Form, Button, Tabs, Radio, Input, Upload, Table, message } from 'antd';
import { LeftSquareTwoTone, UploadOutlined } from '@ant-design/icons';
import { useBoolean } from 'ahooks';
import { If } from 'babel-plugin-jsx-control-statements';
import RequestApi from '@/utils/request';
import RecticationOptions from '../admin-rectication-opinions';
import css from './index.scss';

/**
 * @description 管理员审核上一次结果
 */
const LastAdvice = () => {
	const [data, setData] = useState();
	const columns = [
		{
			title: '整改项',
			dataIndex: 'name',
			key: 'name',
		},
		{
			title: '附件',
			dataIndex: 'attaches',
			key: 'age',
		},
		{
			title: '是否完成(佐证材料)',
			dataIndex: 'id',
			key: 'id',
			render: () => {
				return <h1>404</h1>;
			},
		},
	];
	return (
		<div>
			<p>审核结果：</p>
			<p>审核意见：</p>
			<div>
				<p>审核结果附件：</p>
				<Table dataSource={data} columns={columns} />
			</div>
		</div>
	);
};

/**
 * @description 管理员审核
 */
export default () => {
	const { TabPane } = Tabs;
	const { TextArea } = Input;
	const [state, { toggle }] = useBoolean(true);
	const history = useHistory();
	const search = qs.parse(history.location.search, { ignoreQueryPrefix: true });
	const { name, id } = search;
	// const body = {
	// 	auditName: 'string', // 审核人姓名
	// 	auditReason: 'string', // 审核原因
	// 	auditResultIds: [0], // 文件id
	// 	auditorId: 0, // 审核人id
	// 	reformIdeaDTOS: [
	// 		{
	// 			attachIds: [0], // 审核不通过整改意见文件id
	// 			reformIdea: 'string', // 审核不通过整改意见
	// 		},
	// 	],
	// 	stage: 0, // 所在环节 0在线提交备案信息 1区停车办资料初核 2区停车办现场勘察 3区市镇级备案审核
	// 	status: 0, // 审核结果 0不通过 1通过
	// 	taskId: 0, // 任务id，直接从url的id获取
	// };

	const showDrawer = () => {
		toggle(true);
	};
	const onClose = () => {
		toggle(false);
	};
	const advice = {
		name: '老王',
	};
	const props = {
		beforeUpload: () => {
			return false;
		},
		onChange(info) {
			if (info.file.status !== 'uploading') {
				console.log(info.file, info.fileList);
			}
			if (info.file.status === 'done') {
				message.success(`${info.file.name} file uploaded successfully`);
			} else if (info.file.status === 'error') {
				message.error(`${info.file.name} file upload failed.`);
			}
		},
	};
	const submit = (values) => {
		const body = {};
		body.auditReason = values.auditReason;
		body.status = values.status;
		body.auditResultIds = [];
		body.auditName = Cookies.get('username');
		body.auditId = Cookies.get('id');
		if (name === '资料初审') {
			body.stage = 1;
		} else if (name === '现场勘查') {
			body.stage = 2;
		} else if (name === '复审') {
			body.stage = 3;
		}
		body.taskId = id;
		const reformIdeaDTOS = [];
		for (let i = 1; i <= values.count; i += 1) {
			const obj = {};
			const formData = new FormData();
			formData.append('file', values[`文件_${i}`].file);
			RequestApi.connection('post', `oss/upload/${2}/${0}/${14}`, formData).then((response) => {
				if (response.data === 200) {
					obj.attachIds = [response.data.row.id];
					obj.reformIdea = values[`意见_${i}`];
				}
			});
			reformIdeaDTOS.push(obj);
		}
		body.reformIdeaDTOS = reformIdeaDTOS;
		const formdata = new FormData();
		formdata.append('file', values.auditResult.file);
		RequestApi.connection('post', `oss/upload/${2}/${0}/${14}`, formdata).then((response) => {
			body.auditResultIds = [response.data.row.id];
		});
		console.log(body);
		RequestApi.connection('post', `parkLotRecordAuditRecord/submitAuditRecord`, body)
			.then((response) => {
				message.success('审核成功,即将返回待办中心');
				setTimeout(() => {
					history.push('/todo-kenel');
				}, 500);
			})
			.catch(() => {
				message.error('审核失败');
			});
	};
	const [value, setValue] = useState(1);
	const onChange = (value) => {
		setValue(value.target.value);
	};
	const [form] = Form.useForm();
	return (
		<div className={css.app}>
			<Affix style={{ position: 'absolute', top: '50%', right: 0 }}>
				<Button type="primary" onClick={showDrawer} className={css.left}>
					<LeftSquareTwoTone />
				</Button>
			</Affix>
			<If condition={advice !== undefined}>
				<Drawer width={640} placement="right" closable={false} onClose={onClose} visible={state}>
					<Tabs defaultActiveKey="1" centered>
						<TabPane tab="审核" key="1">
							<Form form={form} onFinish={submit}>
								<Form.Item
									label="审核结果"
									name="status"
									initialValue={1}
									rules={[
										{
											required: true,
											message: '请选择',
										},
									]}
								>
									<Radio.Group onChange={onChange}>
										<Radio value={1}>审核通过</Radio>
										<Radio value={0}>审核不通过</Radio>
									</Radio.Group>
								</Form.Item>
								<Form.Item
									label="审核意见"
									name="auditReason"
									rules={[
										{
											required: true,
											message: '审核意见不能为空',
										},
									]}
								>
									<TextArea allowClear />
								</Form.Item>
								<If condition={value === 0}>
									<p>&nbsp;&nbsp;&nbsp;整改意见:</p>
									<RecticationOptions info={form} />
								</If>
								<Form.Item label="&nbsp;&nbsp;&nbsp;审核结果附件" name="auditResult">
									<Upload {...props}>
										<Button icon={<UploadOutlined />}>选择文件</Button>
									</Upload>
								</Form.Item>

								<Button type="primary" htmlType="submit">
									{value === 1 ? '审核通过' : '提交'}
								</Button>
							</Form>
						</TabPane>
						<TabPane tab="上次审核结果" key="2">
							<LastAdvice />
						</TabPane>
					</Tabs>
				</Drawer>
			</If>
		</div>
	);
};
